
local w,h = 400,400
local w1,h1 = 380,80
local ui = class.panel:builder 
{
    fdf = 'tooltip_backdrop1',
    x = (1920-w)/2,
    y = 200,
    w = w,
    h = h,
    alpha = 0.99,
    show_flag = false,
}
ac.dialog_ui = ui
ui:set_ignore_trackevents(false)

ui.title = ui:add_text('选择奖励',0,10,nil,30,16,5)
ui.buttons = {}
for a=1,6 do
    local button = ui:add_namebutton('物理伤害+50%',(w-w1)/2,50 + (a-1)*(h1+1),w1,h1)
    button.index = a
    ui.buttons[a] = button
end

--点击
function ui:on_button_clicked(button)
    ac.sync('同步-点击对话框',button.index)
    self:hide()
end

--进入
function ui:on_button_mouse_enter(button)
    self.old_select = button
    self.old_select.text:set_color(0xff00ff00)
end

function ui:on_button_mouse_leave()
    if self.old_select then
        self.old_select.text:set_color(0xffffffff)
    end
end

--设置
function ui:set_data(title,data)
    
    if self.old_select then
        self.old_select.text:set_color(0xffffffff)
    end
    for i,button in ipairs(self.buttons) do
        local temp = data[i]
        if temp then
            button.text:set_text(temp.title or temp.name or temp[1])
            button:show()
        else
            button:hide()
        end
    end

    self.title:set_text(title)
    self:set_height(#data*85 + 70)
    self:set_position(self.x,(800-self.h)/2)
    self:show()
end


ac.sync:event '同步-点击对话框'(function(_,player,index)
    local info = player:get_data('当前选择内容')
    local list = player:get_data('选择内容队列') or {}
    local data = info and info[2]
    local func = info and info[3]
    local button = data[index]

    if list[1]==info then
        table.remove(list,1)
    end

    if func and button then
        func(button,index)
    end
    
    local new = list[1]
    if new then
        player:notify('玩家-显示选择框',player,new)
    end
end)

ac.game:event '玩家-显示选择框'(function(_,player,data)
    local info = data[2]
    player:set_data('当前选择内容',data)
    if type(info)=='function' then
        data[2] = info()
    end
    if player:is_self() then
        ui:set_data(data[1],data[2])
    end
end)

ac.game:event '玩家-添加选择框'(function(_,player,title,data,func)
    local list = player:get_data('选择内容队列') or {}
    table.insert(list,{title,data,func})
    player:set_data('选择内容队列',list)
    player:notify('玩家-显示选择框',player,list[1])
end)

